package com.zhang.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录过滤器，这个类就是描述过滤器的规则。
 * 设置请求经过这个过滤器的请求就必须遵守这个过滤器的相关规则
 */
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //将ServletRequest强转为HttpServletRequest，在ServletRequest没有getSession方法
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        Object userBean = req.getSession().getAttribute("userBean");
        if (userBean != null){
            filterChain.doFilter(req,resp);
        }else {
            resp.sendRedirect("/html/login.html?info=no");
        }
    }
}